﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FySystem.Extension
{
    public static class ArrayExtension
    {
        /// <summary>
        /// 判断是否为空，为NULL和长度为0都为true，其余情况为false
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty<T>(this T[] source)
        {
            if (source == null)
                return true;
            else if (source.Length == 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 转换为sql的in查询条件
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="quotes">是否需要单引号</param>
        /// <returns></returns>
        public static string ToSqlInCondition<T>(this T[] arr, bool quotes)
        {
            StringBuilder res = new StringBuilder();
            try
            {
                if (arr.IsNullOrEmpty())
                    return "";

                for (int i = 0; i < arr.Length; i++)
                {
                    if (i > 0)
                        res.Append(',');
                    if (quotes)
                        res.Append($"'{arr[i]}'");
                    else
                        res.Append(arr[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToString();
        }

        /// <summary>
        /// 转换为sql的in查询条件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="arr"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <param name="quotes"></param>
        /// <returns></returns>
        public static string ToSqlInCondition<T>(this T[] arr, int startIndex, int endIndex, bool quotes)
        {
            StringBuilder res = new StringBuilder();
            try
            {
                if (arr.IsNullOrEmpty())
                    return "";

                if (endIndex > arr.Length - 1)
                    endIndex = arr.Length - 1;

                for (int i = startIndex; i <= endIndex; i++)
                {
                    if (i > startIndex)
                        res.Append(',');
                    if (quotes)
                        res.Append($"'{arr[i]}'");
                    else
                        res.Append(arr[i]);
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res.ToString();
        }

        /// <summary>
        /// 获取指定范围的字节数
        /// </summary>
        /// <param name="source"></param>
        /// <param name="begin"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static byte[] ReadBytes(this byte[] source,int begin,int length)
        {
            byte[] res = null;
            try
            {
                int lastIndex = begin + length - 1;
                lastIndex = lastIndex < source.Length ? lastIndex : source.Length - 1;

                res = new byte[lastIndex - begin + 1];

                for (int i = begin; i <= lastIndex; i++)
                {
                    res[i - begin] = source[i];
                }
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }

        public static byte[] ReadBytes(this byte[] source,int begin)
        {
            byte[] res = null;
            try
            {
                if (begin >= source.Length)
                    return res;

                int lastIndex=source.Length - 1;
                res = new byte[lastIndex - begin + 1];

                for (int i = begin; i <= lastIndex; i++)
                    res[i - begin] = source[i];
            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }
}
